<?php

class AccountController extends Zend_Controller_Action {
	public function preDispatch() { 
		$auth = Zend_Auth::getInstance(); 
		if (!$auth->hasIdentity()) { 
			$this->_redirect('login'); 
		}
		else {
			if(!NerdPass_Acl::isAllowed($auth->getIdentity(), 'passwords')) {
				$this->_forward('denied', 'error');
			}
			
		}
	}
	
	public function indexAction() {
		$this->view->categories = NerdPass_Data::getCategories();
		$this->view->passwords = NerdPass_Data::getPasswords();
	}
	
	public function deleteAction() {
		$categories = NerdPass_Data::getCategories();
		$passwords = NerdPass_Data::getPasswords();
		
		// Delete the user's categories,
		foreach($categories as $cat)
		{
			// with all it's passwords
			if (is_array($passwords[$cat['uid']]))
			{
				foreach($passwords[$cat['uid']] as $pswd)
					NerdPass_Data::deletePassword($pswd['uid']);
			}
			NerdPass_Data::deleteCategory($cat['uid']);
		}
		
		// Delete passwords that don't have a category
		if (is_array($passwords[0]))
		{
			foreach($passwords[0] as $pswd)
				NerdPass_Data::deletePassword($pswd['uid']);
		}
		
		// And finally, delete the user
		NerdPass_Data::deleteUser();

		// log the user out
		Zend_Auth::getInstance()->clearIdentity();
	}
}

?>